package com.inventory.api.service;

import com.inventory.api.dao.StockTransactionDao;
import com.inventory.api.entity.StockTransaction;
import com.inventory.api.entity.StockType;
import com.inventory.api.signUpReq.InvntSignupReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lihu on 2018/2/1.
 */
@Service
public class StockTransactionServiceImpl implements StockTransactionService {

    @Autowired
    StockTransactionDao stockTransactionDao;

    @Override
    public void addStockIn(InvntSignupReq signupReq) {
        List<StockTransaction> stockInTransactions = new ArrayList<>();
        for (InvntSignupReq.Inventore inventore : signupReq.getInventores()) {
            StockTransaction stockTransaction = new StockTransaction(inventore,
                    signupReq.getStoreId(), StockType.STOCK_IN, signupReq.getStockSource(),
                    signupReq.getId(), signupReq.getOperatorId(), signupReq.getOperatorName(), signupReq.getNotes());
            stockInTransactions.add(stockTransaction);
            stockInTransactions.add(stockTransaction);
        }
        stockTransactionDao.save(stockInTransactions);
    }

    @Override
    public void addStockOut(InvntSignupReq signupReq) {
        List<StockTransaction> stockInTransactions = new ArrayList<>();
        for (InvntSignupReq.Inventore inventore : signupReq.getInventores()) {
            StockTransaction stockTransaction = new StockTransaction(inventore,
                    signupReq.getStoreId(), StockType.STOCK_OUT, signupReq.getStockSource(),
                    signupReq.getId(), signupReq.getOperatorId(), signupReq.getOperatorName(), signupReq.getNotes());
            stockInTransactions.add(stockTransaction);
            stockInTransactions.add(stockTransaction);
        }
        stockTransactionDao.save(stockInTransactions);
    }
}
